package auth;

import cache.CacheCallBack;
import cache.CacheHelper;
import helper.GlobalConfig;
import jodd.util.StringUtil;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import models.campus.Campus;
import models.user.User;
import models.weixin.WeixinUser;
import org.apache.commons.lang.StringUtils;
import play.Logger;
import play.Play;
import play.mvc.After;
import play.mvc.Before;
import play.mvc.Controller;
import util.common.DateUtil;
import util.common.RandomNumberUtil;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 微信公众号认证，基于OAuth.
 */
public class WxMpAuthTest extends Controller {


    private final static ThreadLocal<User> _currentUser = new ThreadLocal<>();
    private final static ThreadLocal<Campus> _currentCampus = new ThreadLocal<>();


    /**
     * 检查是否已经OAuth认证过，如果没有，则调用认证.
     * @throws Throwable
     */
    @Before(unless = {"login", "logout", "fail", "validate", "validation", "authenticate", "captcha"})
    public static void filter() throws Throwable {
        User user = User.findByUserId("2f2e81d0048b463ea5636d39104806fd");
        _currentUser.set(user);
    }


    @After
    public static void cleanCurrentUser() {
        _currentUser.remove();
    }

    /**
     * 在Controller中使用WexinCpAuth可以得到当前用户.
     */
    public static User currentUser() {
        return _currentUser.get();
    }




    public static void logout(Campus campus) {
        String sessionUserId = session.get(GlobalConfig.WEIXIN_MP_WEIXIN_SESSION_USER_KEY);
        if(StringUtils.isNotBlank(sessionUserId)) {
            CacheHelper.delete(WeixinUser.WXMP_CACHEKEY + sessionUserId);
        }
        session.clear();
    }


    /**
     * 检查是否要跳过认证检查.
     *
     * @return 返回true表明跳过检查.
     */
    private static boolean skipAuthCheck() {
        if (getActionAnnotation(SkipAuth.class) != null ||
                getControllerInheritedAnnotation(SkipAuth.class) != null) {
            Logger.info("skipAuthCheck=true");
            return true;
        }
        Logger.info("skipAuthCheck=false");
        return false;
    }

    /**
     * 检查是否要使用JSAPI.
     *
     * 在类名或方法上使用@UseJsAPI标注，会在renderArgs中加入一个jsSignature的对象.
     *
     * @return UseJsAPI，其value指定需要的权限.
     */
    private static UseJsApi getUseJsAPI() {
        if (getActionAnnotation(UseJsApi.class) != null) {
            Logger.info("UseJsAPI on Action");
            return getActionAnnotation(UseJsApi.class);
        }
        if (getControllerInheritedAnnotation(UseJsApi.class) != null) {
            Logger.info("UseJsAPI on Controller");
            return getControllerInheritedAnnotation(UseJsApi.class);
        }
        Logger.info("UseJsAPI=false");
        return null;
    }

}
